<%@ taglib uri="/linuxbox/common.tld" prefix="common" %>
<%@ taglib uri="/linuxbox/Calendar.tld" prefix="Calendar" %>
<%@ page import="java.util.*,linuxbox.calendar.logic.*,linuxbox.calendar.bean.*,linuxbox.admin.logic.*" %>

<%
  String SORTBY = (String) session.getAttribute("calendar.SORTBY");
  if (SORTBY==null)
  {
    SORTBY = "fromtime";
    session.setAttribute("calendar.SORTBY", SORTBY);
  }

  boolean sortAsc = true;
  tmpStr = (String) session.getAttribute("calendar.ASC");
  if (tmpStr!=null && tmpStr.length() > 0)
    sortAsc = new Boolean(tmpStr).booleanValue();
  else
    session.setAttribute("calendar.ASC", "true");

  int curPage = 0;
  String PAGE = (String) request.getParameter("PAGE");
  if (PAGE != null)
    curPage = Integer.parseInt(PAGE);

  String LIST_CATEGORY = "";
  tmpStr = (String)session.getAttribute("calendar.LIST_CATEGORY");
  if ( tmpStr != null )
    LIST_CATEGORY = tmpStr;

  int LIST_TYPE = BoxCalHome.BOXCALENDAR_LIST_ALL;
  tmpStr = (String)session.getAttribute("calendar.LIST_TYPE");
  if ( tmpStr != null )
  {
    LIST_TYPE = Integer.parseInt(tmpStr);
  }

  int entriesPerPage = sec.getConfigInt("calendar", "Event List", "Entries Per Page");
  Collection col = BoxCalHome.getAllEntries(curID, curCalUserID, LIST_TYPE, LIST_CATEGORY, SORTBY, sortAsc, entriesPerPage*curPage, entriesPerPage);
  int totalPage = (col.size() / entriesPerPage) +1;
  Iterator it = null;
  if (col != null)
    it = col.iterator();

  String calendarJSP = BoxStatic.jspDir+"calendar/BoxCalendar.jsp";
  String calendarAction = BoxStatic.servletDir+"BoxCalendarAction";
%>

<script Language="JavaScript">

  function onChangeCategory()
  {
    document.changeCategoryForm.submit();
  }
</script>

<table width="100%" border=0 cellpadding=0 cellspacing=0>
  <tr>
    <td>
	  	<table width="100%">
    		<tr>
          <form name="changeCategoryForm" method="POST" action="<%=BoxStatic.servletDir%>BoxCalendarAction">
      		<td align="left"> View:
<%
            	if ( LIST_TYPE == BoxCalHome.BOXCALENDAR_LIST_ALL )
              {
                out.print("All | ");
              }
              else
              {
                out.print("<a href=\"");
                out.print(BoxStatic.servletDir);
                out.print("BoxCalendarAction?v=list&list=");
                out.print(BoxCalHome.BOXCALENDAR_LIST_ALL);
                out.print("\">All</a> |");
              }

              if ( LIST_TYPE == BoxCalHome.BOXCALENDAR_LIST_UPCOMING )
              {
                out.print("Upcoming | ");
              }
              else
              {
                out.print("<a href=\"");
                out.print(BoxStatic.servletDir);
                out.print("BoxCalendarAction?v=list&list=");
                out.print(BoxCalHome.BOXCALENDAR_LIST_UPCOMING);
                out.print("\">Upcoming</a> |");
              }

            	if ( LIST_TYPE == BoxCalHome.BOXCALENDAR_LIST_PAST)
              {
                out.print("Past | ");
              }
              else
              {
                out.print("<a href=\"");
                out.print(BoxStatic.servletDir);
                out.print("BoxCalendarAction?v=list&list=");
                out.print(BoxCalHome.BOXCALENDAR_LIST_PAST);
                out.print("\">Past</a> |");
              }
%>
            <select name="LIST_CATEGORY" onchange="onChangeCategory()">
             	<Calendar:CalendarCatList selected="<%=LIST_CATEGORY%>" showsystemcat="true" showallcategories="true" userid="<%=curCalUserID%>"/>
            </select>
      		</td>
          </form>
  				<td align=right>
    				<common:Page curPage="<%=curPage%>" totalPage="<%=totalPage%>" action="<%=calendarJSP%>" />
  				</td>
        </tr>
      </table>
    </td>
	</tr>

<%
  if ( it != null && it.hasNext())
  {
    String[] headers = {"Date", "Time", "Subject", "Category", "Delete"};
    String[] sortby = {"fromtime", "", "subject", "category",  ""};
%>
  <TR align=center bgcolor=dddddd>
    <TD align=center>
      <TABLE width="100%" cellpadding=2 cellspacing=1 border=0>

		    <TR bgColor=#7EC1F3>
    		  <common:ListHeader headers="<%=headers%>" sortby="<%=sortby%>" curSortBy="<%=SORTBY%>" sortAsc="<%=sortAsc%>" action="<%=calendarAction%>" />
    		</TR>
      <common:Iteration name="entry" type="linuxbox.calendar.logic.BoxCalendarEntry" group="<%=it%>">
        <TR bgcolor=ffffff>
          <TD align=left width="1%" nowrap>
            <FONT size=-1>
            <a href="<%=BoxStatic.servletDir%>BoxCalendarAction?v=day&t=<%=entry.getFromTime().getTime()%>"><%=StringUtil.getDateTimeFormatString(sec.getTimeZone(), BoxStatic.SHORT_DATE_FORMAT, entry.getFromTime())%></a>
            </FONT>
          </TD>
          <TD align=left width="1%" nowrap>
            <FONT size=-1> <%=entry.getTimeRangeString(sec.getTimeZone())%></FONT>
          </TD>
          <TD>
            <Calendar:CalendarEntry timezone="<%=sec.getTimeZone()%>" viewerid="<%=curID%>" showtime="false" showdelete="false" calendarentry="<%=entry%>"/>
          </TD>
          <TD align=left width="1%" nowrap>
            <%=entry.getCategory()%>
          </TD>
          <TD align=center width="1%">
<%
					if (entry.getUserID() == curID)
          {
%>
            <a href="<%=BoxStatic.servletDir%>BoxCalendarAction?SubmitDelete=del&v=list&id=<%=entry.getID()%>"
            Onclick='return confirmDel("<%=entry.getSubject()+ " " + entry.getTimeRangeString(sec.getTimeZone())%>")'
            ALT="Delete">Delete</a>
<%
          }
          else
             out.print("&nbsp;");
%>
          </TD>
        </TR>
      </common:Iteration>
      </TABLE>
    </TD>
  </TR>
	<tr>
  	<td align=right>
    	<common:Page curPage="<%=curPage%>" totalPage="<%=totalPage%>" action="<%=calendarJSP%>" />
  	</td>
	</tr>


<%
    }
    else
    {
      out.print("<tr><td align=center><h2>No events were found!</h2></td></tr>");
    }
%>
  <TR bgcolor=ffffff>
    <TD bgcolor=ffffff align=center>
    <FONT color=ffffff> --- --- --- --- --- --- --- --- --- --- ---
    --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
    --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
    </FONT>
    </TD>
  </TR>
</TABLE>
